#include <mutex>

#include "user_team_role.h"

namespace Elements
{

UserTeamRole::UserTeamRole(const UserTeamRole& other)
    : Element(other)
    , m_userId(other.m_userId)
    , m_teamId(other.m_teamId)
    , m_roleId(other.m_roleId)
{
}

bool UserTeamRole::isSame(Element* const other) const
{
    const auto otherElement = dynamic_cast<UserTeamRole const*>(other);
    if (nullptr == otherElement)
        return false;

    return bool(
        m_id == otherElement->m_id
        && m_userId == otherElement->m_userId
        && m_teamId == otherElement->m_teamId
        && m_roleId == otherElement->m_roleId
    );
}

Common::Json UserTeamRole::toJson() const
{
    std::shared_lock locker(m_lock);
    Common::Json result;
    result["id"] = m_id;
    result["elementType"] = unsigned(elementType());
    result["userId"] = m_userId;
    result["teamId"] = m_teamId;
    result["roleId"] = m_roleId;
    return result;
}

void UserTeamRole::fromJson(const Common::Json& json)
{
    std::shared_lock locker(m_lock);
    m_id = json["id"].get<unsigned>();
    m_userId = json["userId"].get<unsigned>();
    m_teamId = json["teamId"].get<unsigned>();
    m_roleId = json["roleId"].get<unsigned>();
}

Element* UserTeamRole::clone() const
{
    return new UserTeamRole(*this);
}

ElementType UserTeamRole::elementType() const
{
    return ElementType::UserTeamRole;
}

unsigned UserTeamRole::userId() const
{
    std::shared_lock locker(m_lock);
    return m_userId;
}

void UserTeamRole::setUserId(const unsigned value)
{
    std::unique_lock locker(m_lock);
    m_userId = value;
}

unsigned UserTeamRole::teamId() const
{
    std::shared_lock locker(m_lock);
    return m_teamId;
}

void UserTeamRole::setTeamId(const unsigned value)
{
    std::unique_lock locker(m_lock);
    m_teamId = value;
}

unsigned UserTeamRole::roleId() const
{
    std::shared_lock locker(m_lock);
    return m_roleId;
}

void UserTeamRole::setRoleId(const unsigned value)
{
    std::unique_lock locker(m_lock);
    m_roleId = value;
}

} // namespace Elements
